// 云函数入口文件
const cloud = require('wx-server-sdk')
const request = require('request');
// const axios = require('axios');
// var rp = require('request-promise');
cloud.init()


// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  let data = event.data
  data.openid = wxContext.OPENID
  data.appid= wxContext.APPID


  return new Promise((resolve,reject) => {


    request({
      url: event.url + "?" + jsonToKeyANDValueString(data),
      method: event.method,
      json: true,
      dataType: 'json',
      responseType: 'json',
    }, (err, resp, body) => {
      if (err) {
        return reject(err)
      }
      return resolve(resp)
    })
  })
  

}
function jsonToKeyANDValueString(data){
  let str = ''
  for (let i in data) {
    str += i + '=' + data[i] + "&"
  }
  return str
}